package com.example.demo.controller;

import com.example.demo.log.Log;
import com.example.demo.log.LogModuleEnum;
import com.example.demo.model.core.Add;
import com.example.demo.model.core.R;
import com.example.demo.model.core.Update;
import com.example.demo.model.form.PermissionForm;
import com.example.demo.model.query.PermissionQuery;
import com.example.demo.model.vo.OptionVO;
import com.example.demo.model.vo.PermissionVO;
import com.example.demo.model.vo.RouteVO;
import com.example.demo.security.util.SecurityUtils;
import com.example.demo.service.SysPermissionService;
import com.example.demo.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/v1/perm")
@Tag(name = "权限接口")
public class SysPermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('sys:perm:add')")
    @Operation(summary = "新增权限")
    @Log(value = "新增权限",module = LogModuleEnum.Perms)
    public R<Void> add(@Validated(Add.class) @RequestBody PermissionForm form) {
        sysPermissionService.add(form);
        return R.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('sys:perm:edit')")
    @Operation(summary = "修改权限")
    @Log(value = "修改权限",module = LogModuleEnum.Perms)
    public R<Void> update(@Validated(Update.class) @RequestBody PermissionForm form) {
        sysPermissionService.update(form);
        return R.ok();
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPerm('sys:perm:delete')")
    @Operation(summary = "删除权限")
    @Log(value = "删除权限",module = LogModuleEnum.Perms)
    public R<Void> delete(@RequestBody List<String> ids) {
        sysPermissionService.delete(ids);
        return R.ok();
    }

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPerm('sys:perm:query')")
    @Operation(summary = "权限列表")
    public R<List<PermissionVO>> list(@Validated PermissionQuery query) {
        List<PermissionVO> page = sysPermissionService.list(query);
        return R.ok(page);
    }

    @GetMapping("/form")
    @Operation(summary = "权限表单")
    public R<PermissionForm> form(String id) {
        PermissionForm form = sysPermissionService.form(id);
        return R.ok(form);
    }

    @PostMapping("/routes")
    @Operation(summary = "用户权限路由")
    public R<List<RouteVO>> routes() {
        Set<String> roleIds = SecurityUtils.getRoles();
        List<RouteVO> routes = sysRoleService.getRoleRoutes(roleIds);
        return R.ok(routes);
    }

    @GetMapping("/options")
    @Operation(summary = "权限树")
    public R<List<OptionVO<String>>> options(
            @Parameter(description = "是否只查询父级菜单")
            @RequestParam(required = false, defaultValue = "false") boolean onlyParent
    ) {
        List<OptionVO<String>> menus = sysPermissionService.options(onlyParent);
        return R.ok(menus);
    }


}
